﻿using CHS.ChengDu.Service;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using CHS.ChengDu.Service.CryptoUtils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Security.Cryptography.X509Certificates;

namespace Com.WebApiServe.Controllers
{
    [RoutePrefix("Api/CHSUtil")]
    public class CHSUtilController:ApiController
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="_input"></param>
        /// <returns></returns>
        [Route("Encrypt")]
        [HttpPost]
        public CHSEncryptionOutput Encrypt(CHSEncryptionInput _input)
        {
            if (string.IsNullOrEmpty(_input.AppId))
            {
                return new CHSEncryptionOutput { Code = -1, Message = "AppId不能为空" };
            }

            if (string.IsNullOrEmpty(_input.AppSercret))
            {
                return new CHSEncryptionOutput { Code = -1, Message = "AppSercret不能为空" };
            }

            if (string.IsNullOrEmpty(_input.Input))
            {
                return new CHSEncryptionOutput { Code = -1, Message = "Input不能为空" };
            }
            string encData = "";
            try
            {
                JObject jInputObj = JObject.Parse(_input.Input);

                 encData = SMUtil.encrypt(JsonConvert.SerializeObject(jInputObj, Formatting.None), _input.AppId, _input.AppSercret);
            }
            catch (Exception e)
            {
                return new CHSEncryptionOutput { Code = -1, Message = "加密发生异常:" + e.Message };
            }
            

            return new CHSEncryptionOutput {output = encData };
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="_input"></param>
        /// <returns></returns>
        [Route("Decrypt")]
        [HttpPost]
        public CHSEncryptionOutput Decrypt(CHSEncryptionInput _input)
        {
            if (string.IsNullOrEmpty(_input.AppId))
            {
                return new CHSEncryptionOutput { Code = -1, Message = "AppId不能为空" };
            }

            if (string.IsNullOrEmpty(_input.AppSercret))
            {
                return new CHSEncryptionOutput { Code = -1, Message = "AppSercret不能为空" };
            }

            if (string.IsNullOrEmpty(_input.Input))
            {
                return new CHSEncryptionOutput { Code = -1, Message = "Input不能为空" };
            }

            String encData = SMUtil.decrypt(_input.Input, _input.AppId, _input.AppSercret);

            return new CHSEncryptionOutput { output = encData };
        }

        /// <summary>
        /// 签名
        /// </summary>
        /// <param name="_input">交易输入</param>
        /// <returns></returns>
        [Route("Sign")]
        [HttpPost]
        public CHSEncryptionOutput Sign(CHSEncryptionInput _input)
        {

            if (string.IsNullOrEmpty(_input.AppSercret))
            {
                return new CHSEncryptionOutput { Code = -1, Message = "AppSercret不能为空" };
            }

            if (string.IsNullOrEmpty(_input.Input))
            {
                return new CHSEncryptionOutput { Code = -1, Message = "Input不能为空" };
            }

            if (string.IsNullOrEmpty(_input.PrivateKey))
            {
                return new CHSEncryptionOutput { Code = -1, Message = "PrivateKey不能为空" };
            }
            string signData = "";
            try
            {
                JObject jInput = JObject.Parse(_input.Input);

                signData = SMUtil.sign(jInput, _input.AppSercret, _input.PrivateKey);
            }
            catch (Exception e)
            {
                return new CHSEncryptionOutput { Code = -1, Message = "签名异常，异常信息" + e.Message };
            }

            return new CHSEncryptionOutput { output = signData };
        }
        /// <summary>
        /// 验签
        /// </summary>
        /// <param name="_input"></param>
        /// <returns></returns>
        [Route("VerifySign")]
        [HttpPost]
        public CHSEncryptionOutput VerifySign(CHSEncryptionInput _input)
        {
            if (string.IsNullOrEmpty(_input.AppSercret))
            {
                return new CHSEncryptionOutput { Code = -1, Message = "AppSercret不能为空" };
            }

            if (string.IsNullOrEmpty(_input.Input))
            {
                return new CHSEncryptionOutput { Code = -1, Message = "Input不能为空" };
            }

            if (string.IsNullOrEmpty(_input.PublicKey))
            {
                return new CHSEncryptionOutput { Code = -1, Message = "PublicKey不能为空" };
            }
            bool result = false;

            try
            {
                JObject jInput = JObject.Parse(_input.Input);

                result = SMUtil.verify(jInput, _input.AppSercret, _input.PublicKey, _input.SignInput);
            }
            catch (Exception e)
            {
                return new CHSEncryptionOutput { Code = -1, Message = "验签，异常信息" + e.Message };
            }
            return new CHSEncryptionOutput { output = "" };
        }

    }
}
